// import Router from './useRouter';

const formatTime = date => {
  /*  console.log(time)
    let date = new Date(time);*/
    // console.log(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const format_time = date => {
    /*  console.log(time)
      let date = new Date(time);*/
    // console.log(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

function getTime (timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [hour, minute].map(formatNumber).join(':')
}

function timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() ;
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) ;
    var D = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    // return Y+M+D+' '+h+m+s;
    return [Y, M, D].map(formatNumber).join('-')
/*        + ' ' + [h, m, s].map(formatNumber).join(':')*/
}
function timestampToHSM(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() ;
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) ;
    var D = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    // return Y+M+D+' '+h+m+s;
    return [Y, M, D].map(formatNumber).join('-')
            + ' ' + [h, m].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
/*
 * obj 转 路由地址带参数
 * 例子：{a:1,b:2} => /pages/index/index?a=1&b=2
 */
export function objParseUrlAndParam(path, obj) {
    // console.log(obj);

    let url = path || '/';
    let paramsStr = '';

    for (let key in obj) {
        paramsStr += `${key}=${obj[key]}&`
    }
    paramsStr = paramsStr.substring(0, paramsStr.length - 1);

    if (paramsStr) {
        if (url.indexOf('?') === -1) {
            url = `${url}?${paramsStr}`
        } else {
            url = url.replace('?', `?${paramsStr}&`)
        }
    }

    return url
}
export function formatDate(timestamp, formats) {
    /*
    formats格式包括
    1. Y-M-D
    2. Y-M-D h:m:s
    3. Y年M月D日
    4. Y年M月D日 h时m分
    5. Y年M月D日 h时m分s秒
    示例：console.log(formatDate(1500305226034, 'Y年M月D日 h:m:s')) ==> 2017年07月17日 23:27:06
     */
    formats = formats || 'Y-M-D'

    var myDate = timestamp ? new Date(timestamp) : new Date()

    var year = myDate.getFullYear()
    var month = formatDigit(myDate.getMonth() + 1)
    var day = formatDigit(myDate.getDate())

    var hour = formatDigit(myDate.getHours())
    var minute = formatDigit(myDate.getMinutes())
    var second = formatDigit(myDate.getSeconds())

    var ddd = {
        Y: year,
        M: month,
        D: day,
        h: hour,
        m: minute,
        s: second
    }
    return formats.replace(/Y|M|D|h|m|s/g, function(matches) {
        return ddd[matches]
    })
    // 小于10补0
    function formatDigit(n) {
        return n.toString().replace(/^(\d)$/, '0$1')
    }
}

export function SecondToDate(msd) {
  var time =msd
  if (null != time && "" != time) {
    if (time > 60 && time < 60 * 60) {
      time = parseInt(time / 60.0) + ":" + parseInt((parseFloat(time / 60.0) -
        parseInt(time / 60.0)) * 60);
    }
    else if (time >= 60 * 60 && time < 60 * 60 * 24) {
      time = parseInt(time / 3600.0) + ":" + parseInt((parseFloat(time / 3600.0) -
        parseInt(time / 3600.0)) * 60) + ":" +
        parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
          parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60);
    } else if (time >= 60 * 60 * 24) {
      time = parseInt(time / 3600.0/24) + ":" +parseInt((parseFloat(time / 3600.0/24)-
        parseInt(time / 3600.0/24))*24) + ":" + parseInt((parseFloat(time / 3600.0) -
        parseInt(time / 3600.0)) * 60) + ":" +
        parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
          parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60);
    }
    else {
      time = parseInt(time);
    }
  }
  return time;
}

export default  {
	SecondToDate,
    formatDate,
    formatTime: formatTime,
    timestampToTime:timestampToTime,
    getTime:getTime,
    format_time:format_time,
    timestampToHSM:timestampToHSM
    // Router,
}


/**
 * 获取应用使用平台标识
 */
/*export function getPlatformType() {
    // #ifdef APP-PLUS
    return 'app'
    // #endif
    // #ifdef MP-WEIXIN
    return 'wxapp'
    // #endif
    // #ifdef H5
    return 'h5'
    // #endif
    return 'h5'
}*/

/**
 * 获取区分h5平台的具体环境
 */
/*export function getPlatform() {
    let user_platform = getPlatformType()
    if (user_platform == 'h5') {
        let ua = navigator.userAgent.toLowerCase()
        let isWeixin = ua.indexOf('micromessenger') != -1
        if (isWeixin) {
            user_platform = 'wx'
        }
    }

    return user_platform
}*/
